package com.ucpaas.monitor.controller;

import com.alibaba.fastjson.JSONObject;
import com.ucpaas.monitor.common.JsonDto;
import com.ucpaas.monitor.service.MonitorService;
import com.ucpaas.monitor.util.BootUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class MonitorController {
	
	private static final Logger logger = LoggerFactory.getLogger(MonitorController.class);
	
	@Autowired
    private MonitorService monitorService;
	
	 @RequestMapping("/getIP")
     @ResponseBody
     public JsonDto getIP(HttpServletRequest request) {
		 JsonDto jsonDto = new JsonDto();
		 String ip = BootUtils.getIpAddress(request);
		 logger.info("ip is :{}",ip);
		try {
			jsonDto.setCode("0");
			jsonDto.setData(ip);
		} catch (Exception e) {
			e.printStackTrace();
			jsonDto.setCode("-1");;
		}
         return  jsonDto;
     }
	 
//	 @RequestMapping("/monitorVM")
//	 public void monitor() throws Exception {
//		 monitorService.getRemoteVM();
//	 }

	/**
	 * 重启CC
	 * @throws Exception
	 */
	 @RequestMapping("/command")
	 @ResponseBody
	 public JsonDto command(HttpServletRequest request) throws Exception {
	 	JsonDto jsonDto = new JsonDto();
		 JSONObject jsonObj =  BootUtils.getJSONParam(request);
		 try{
			 jsonDto = monitorService.command(jsonObj);
		 }catch (Exception e){
			 jsonDto.setCode("-1");
			 jsonDto.setMsg(e.getMessage());
		 }
		 return jsonDto;
	 }

}
